<?php
namespace Application\model;


use Cola\Model;

class PlayerModel extends Model
{
    protected function requestAll()
    {
        $json_data = file_get_contents('php://input');
        return json_decode($json_data, true);
    }
    public function get($k, $default = '')
    {
        $data = $_GET[$k] ?? ($_POST[$k] ?? $default);

        return $data;
    }

    protected function result($code=0, $msg='', $content=[])
    {
        return [
            'ret' => $code,
            'msg' => $msg,
            'content' => $content
        ];
    }

    protected function sign($rows, $sign, $secret='')
    {
        if(isset($rows['sign'])) unset($rows['sign']);
        ksort($rows);
        // 生成字符串
        $signStr = [];
        foreach ($rows as $key=>$value) {
            $signStr[] = $key . '=' . $value;
        }
        $str = implode('&', $signStr) . $secret;;
        $mySign = md5($str);
        if ($sign != $mySign) {
            // 打印日志
            $this->logfile("sign err. get sign:{$sign} md5:{$mySign} str:{$str}");
            return false;
        }
        return true;
    }

    protected function banParam($rows)
    {
        return $rows;
    }

    protected function curlWay($url,$data,$is_json=true)
    {
        $ch = curl_init($url); // 替换为目标API的URL
        $params = $is_json ? json_encode($data) : http_build_query($data);
        // 设置必要的cURL选项
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是直接输出
        curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // 设置POST字段

        if($is_json){
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json', // 设置内容类型为JSON
                'Content-Length: ' . strlen($params) // 设置内容长度
            ));
        }
        // 执行cURL会话并获取响应
        $response = curl_exec($ch);

        // 检查是否有错误发生
        if (curl_errno($ch)) {
            return 'Error:' . curl_error($ch);
        }

        // 关闭cURL资源，并释放系统资源
        curl_close($ch);
        // 处理响应
        return $response;
    }

    public function serverList($row)
    {
        $cpid = $row['cpid'] ?? '';
        $page = $row['page'] ?? 1;
        $pageSize = $row['pageSize'] ?? 1000;
        $where = $this->model('loginModel')->getChannelServerMap('', $cpid);

        $where .= " and open_time<='".date('Y-m-d H:i:s')."'";
        $data = $this->model('Server')->getPage($page, $pageSize, $where);
        $content = [
            'total_page' => 0,
            'server_list' => [],
        ];
        if ($data['servers']) {
            $servers = [];
            foreach ($data['servers'] as $server) {
                $openTime = strtotime(date('Y-m-d', strtotime($server['open_time'])));
                $time = strtotime(date('Y-m-d'));
                $diff = floor(abs($openTime - $time) / 86400)+1;
                $arr = [];
                $arr['server_name'] = $server['name'];
                $arr['server_num'] = $server['sid'];
                $arr['server_created_at'] = $server['open_time'];
                $arr['merge_count'] = 0;
                $arr['open_days'] = $diff;
                $arr['merge_days'] = 0;
                $arr['merge_at'] = '';
                $arr['master'] = $server['sid'];
                $servers[] = $arr;
            }
            $content = [
                'total_page' => $data['total'],
                'server_list' => $servers,
            ];
        }
        return $content;
    }





}
